<?php
/*
 * inc-browseQuestions.php
 * Retral/CSCD494 Winter 2009
 * Eastern Washington University
 *
 * File Description:
 * 
 * This file is used to browse and search all the current questions
 * in the database. 
 *
 * Other:
 *
 */


/* 
 * Set up $page_name
 * This name will be compared to PAGE_NAME in NAV_URLS table (stored in session) and verify permission for given user.
 */
$page_name = "Browse Questions";
$page_name = strtoupper($page_name);
//Print_r($_SESSION);
require('KinID.php');

// Is user logged in, and has permissions?
if (isset($_SESSION['loggedIn']) && $_SESSION['loggedIn'] !="" && $_SESSION['pagePermissions'][$page_name] == 1)
{
?>

	<span class="header-text">QUESTIONS</span><br>

	<h2>Browse Questions</h2>
	<?php
		// connection to be used to connect to the database
        // ORA_CON_UN, ORA_CON_PW, ORA_CON_DB are global variables
        $conn = oci_connect(ORA_CON_UN, ORA_CON_PW, ORA_CON_DB);
	?>
	
	<table border=0 width=100%>
		<tr>
			<td><u><a href='?page=browseQuestions&sort=title'>Question Title</a></u></td><td width=150><u><a href='?page=browseQuestions&sort=type'>Question Type</a></u></td>
			<td width=150><u><a href='?page=browseQuestions&sort=author'>Author</a></u></td><td width=80><u><a href='?page=browseQuestions&sort=points'>Points</a></u></td>
		</tr>

    <?php
	
	$sort = " ORDER BY question_title ";

	if(isset($_REQUEST['sort'])) {
		if($_REQUEST['sort'] == "type")
			$sort = " ORDER BY type_description";
		else if($_REQUEST['sort'] == "author")
			$sort = " ORDER BY last_name";
		else if($_REQUEST['sort'] == "points")
			$sort = " ORDER BY points DESC";
	}	
	
	if(isset($_POST['type']))
		$sort = " AND question_type.type_description = ".$_POST['type'];
		
	$query = "SELECT question_id,q_version, MAX(Q_REVISION), type_description, question_title,last_name, points FROM 
(Select question.q_revision, question.q_version,question.question_id,question_content.question_title, question_type.type_description, 
            ape_user.last_name, question_content.points from question 
           INNER JOIN question_content ON question.question_content_id=question_content.question_content_id 
           INNER JOIN ape_user ON question_content.author_id=ape_user.user_id 
            INNER JOIN question_type ON question.question_type_code=question_type.type_code 
           WHERE question.q_revision >0) GROUP BY question_id,q_version, question_title,type_description,question_title,last_name,points ".$sort;
			
	//Part of potential replacement SELECT question_id,q_version, MAX(Q_REVISION) FROM (SELECT QUESTION_ID, q_version, q_revision from question) group by question_id,q_version;
	
	if(isset($_POST['submit']))
	{
	}
	$stmt = oci_parse($conn,$query);
	if(!oci_execute($stmt)) {
			print "<P>Could not find question list</P>";
		}
	
	while($result = oci_fetch_assoc($stmt))
	{   
		echo "<tr>";
		echo("<td>");
		echo "<a href='?page=question&id=".$result["QUESTION_ID"]."&ver=".$result["Q_VERSION"]."'>";
		echo $result["QUESTION_TITLE"];
		echo("</a></td>");
		echo("<td>");
		echo $result["TYPE_DESCRIPTION"];
		echo("</td>");
		echo("<td>");
		echo $result["LAST_NAME"];
		echo("</td>");
		echo("<td>");
		echo $result["POINTS"];
		echo("</td>");

        //if we are in the question selection cycle
        if($_SESSION['select'] == 1)
            {
 
            echo "<form action ='?page=preview&exam=".$_SESSION["selectExam"]."' method = 'POST'>";
            echo("<td><input type='submit' class='button' value='Select'/></td>");
            echo("<input type='hidden' value='".$result['QUESTION_ID']."'name='questionID'> ");
            echo("<input type='hidden' value='".$result['Q_VERSION']."'name='questionVersion'> ");
            echo"</form>";
    }
        echo "</tr>";
        
		
	}
	print "</table>";
		oci_free_statement($stmt);
		oci_close($conn);
}
else
{
	echo "Invalid credentials";
	echo "<META http-equiv=\"refresh\" content=\"0;URL=index.php?page=login&ld=0\">";
}

?>
